import moment from 'moment'
import request from '@/utils/request'
import qs from 'qs'

const tableMixin = {
  data: function () {
    return {
      // pagination
      pagination: {
        total: 0,
        current: 1,
        size: 10,
        sizes: [10, 20, 30, 40]
      },
      tableData: [],
      delVisualData: [],
      loading: false,
      operationLock: false,
      showMore: false
    }
  },
  computed: {
    formSearch () {
      return this.modelSearch
    }
  },
  methods: {
    tableHeaderStyle ({ row, column, rowIndex, columnIndex }) {
      return rowIndex === 0 ? 'background-color: #FAFAFA;font-size: 14px;color: #4B4B4B;font-family: PingFangSC-Medium;' : ''
    },
    tableCellStyle ({ row, rowIndex }) {
      return 'font-size: 12px;color: #4B4B4B;'
    },
    async handleSearch () {
      try {
        await this.$refs.formSearch.validate()
      } catch (e) {
        return
      }
      this.pagination.current = 1
      this.tableReload(this.formSearch)
    },
    handleReset () {
      const form = this.$refs.formSearch
      if (form) {
        form.resetFields()
      }
      this.pagination.current = 1
      this.tableReload(this.formSearch)
    },
    handleCurrentChange (current) {
      this.pagination.current = current
      this.tableReload(this.formSearch)
    },
    handleSizeChange (size) {
      this.pagination.size = size
      this.tableReload(this.formSearch)
    },
    clickShowMore (e) {
      this.showMore = !this.showMore
    },
    async tableReload (condition = this.formSearch) {
      this.loading = true
      let res = null
      let resourceLocation = null
      if (this.api && this.api.resourceLocation) {
        resourceLocation = this.api.resourceLocation
      } else if (this.api && this.api.listUrl) {
        resourceLocation = this.api.listUrl
      }
      try {
        const conditionBak = { ...condition }
        /* Object.keys(conditionBak).forEach(key => {
          conditionBak[key] = encodeURIComponent(conditionBak[key])
        }) */
        const params = this.lodash.merge({}, conditionBak, {
          pageNum: this.pagination.current,
          pageSize: this.pagination.size
        })
        for (const [key, value] of Object.entries(params)) {
          if (value === '') {
            delete params[key]
          }
        }
        res = await request({
          method: 'get',
          url: resourceLocation,
          params,
          paramsSerializer: params => {
            return qs.stringify(params, { arrayFormat: 'repeat' })
          }
        })
      } catch (e) {
        this.loading = false
      }
      if (res && res.data) {
        this.tableData = res.data.records
        this.pagination.total = res.data.total
        this.addVisual(res.data.records)
      }
      this.loading = false
    },
    // 手动控制tooltip，避免MessageBox关闭导致tooltip自动触发
    addVisual: function (dataList) {
      this.delVisualData = []
      for (let i = 0; i < dataList.length; i++) {
        this.delVisualData.push({ visible: false })
      }
    },
    // formatter
    dateFormatter: (row, column, cellValue) => {
      return cellValue ? moment(cellValue).format('yyyy-MM-dd') : ''
    },
    booleanFormatter: (row, column, cellValue) => {
      // eslint-disable-next-line eqeqeq
      if (cellValue == 1) {
        return '是'
      // eslint-disable-next-line eqeqeq
      } else if (cellValue == 0) {
        return '否'
      }
    },
    getSession () {
      const sessionSearch = sessionStorage.getItem('searchParams')
      const sessionPage = sessionStorage.getItem('pagination')
      if (sessionSearch) {
        const formSearch = JSON.parse(sessionSearch)
        this.modelSearch = Object.assign({}, this.modelSearch, formSearch)
      }
      if (sessionPage) {
        const pageInfo = JSON.parse(sessionPage)
        this.pagination = Object.assign({}, this.pagination, pageInfo)
      }
    },
    setSession () {
      sessionStorage.setItem('searchParams', JSON.stringify(this.formSearch))
      sessionStorage.setItem('pagination', JSON.stringify(this.pagination))
    }
  },
  mounted: function () {
    this.$nextTick(() => {
      this.getSession()
      sessionStorage.clear()// session封闭使用，用完即清空，防止污染模块
      this.tableReload(this.formSearch)
    })
  }
}
export default tableMixin
